package pe.gob.mpfn.model;

import java.util.List;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.PersistenceContext;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;
import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.roo.addon.dbre.RooDbManaged;
import org.springframework.roo.addon.javabean.RooJavaBean;
import org.springframework.roo.addon.jpa.activerecord.RooJpaActiveRecord;
import org.springframework.roo.addon.tostring.RooToString;
import org.springframework.transaction.annotation.Transactional;

@Entity
@Table(name = "parte")
@Configurable
@RooJavaBean
@RooToString
@RooJpaActiveRecord(versionField = "", table = "parte")
@RooDbManaged(automaticallyDelete = true)
public class Parte {

	@OneToMany(mappedBy = "intParteId")
    private Set<ExpedienteParte> expedientePartes;

	@OneToMany(mappedBy = "intParteId")
    private Set<Proteccion> proteccions;

	@ManyToOne
    @JoinColumn(name = "int_tipo_documento_id", referencedColumnName = "int_tabla_id", nullable = false)
    private TablaMaestra intTipoDocumentoId;

	@ManyToOne
    @JoinColumn(name = "int_tipo_parte_id", referencedColumnName = "int_tabla_id", nullable = false)
    private TablaMaestra intTipoParteId;

	@Column(name = "str_nombre", columnDefinition = "VARCHAR", length = 45)
    @NotNull
    private String strNombre;

	@Column(name = "str_apellido_parterno", columnDefinition = "VARCHAR", length = 45)
    @NotNull
    private String strApellidoParterno;

	@Column(name = "str_apellido_materno", columnDefinition = "VARCHAR", length = 45)
    @NotNull
    private String strApellidoMaterno;

	@Column(name = "int_edad", columnDefinition = "INT")
    @NotNull
    private Integer intEdad;

	@Column(name = "str_numero_documento", columnDefinition = "VARCHAR", length = 50)
    @NotNull
    private String strNumeroDocumento;

	@Column(name = "str_estado", columnDefinition = "CHAR")
    private Character strEstado;

	public Set<ExpedienteParte> getExpedientePartes() {
        return expedientePartes;
    }

	public void setExpedientePartes(Set<ExpedienteParte> expedientePartes) {
        this.expedientePartes = expedientePartes;
    }

	public Set<Proteccion> getProteccions() {
        return proteccions;
    }

	public void setProteccions(Set<Proteccion> proteccions) {
        this.proteccions = proteccions;
    }

	public TablaMaestra getIntTipoDocumentoId() {
        return intTipoDocumentoId;
    }

	public void setIntTipoDocumentoId(TablaMaestra intTipoDocumentoId) {
        this.intTipoDocumentoId = intTipoDocumentoId;
    }

	public TablaMaestra getIntTipoParteId() {
        return intTipoParteId;
    }

	public void setIntTipoParteId(TablaMaestra intTipoParteId) {
        this.intTipoParteId = intTipoParteId;
    }

	public String getStrNombre() {
        return strNombre;
    }

	public void setStrNombre(String strNombre) {
        this.strNombre = strNombre;
    }

	public String getStrApellidoParterno() {
        return strApellidoParterno;
    }

	public void setStrApellidoParterno(String strApellidoParterno) {
        this.strApellidoParterno = strApellidoParterno;
    }

	public String getStrApellidoMaterno() {
        return strApellidoMaterno;
    }

	public void setStrApellidoMaterno(String strApellidoMaterno) {
        this.strApellidoMaterno = strApellidoMaterno;
    }

	public Integer getIntEdad() {
        return intEdad;
    }

	public void setIntEdad(Integer intEdad) {
        this.intEdad = intEdad;
    }

	public String getStrNumeroDocumento() {
        return strNumeroDocumento;
    }

	public void setStrNumeroDocumento(String strNumeroDocumento) {
        this.strNumeroDocumento = strNumeroDocumento;
    }

	public Character getStrEstado() {
        return strEstado;
    }

	public void setStrEstado(Character strEstado) {
        this.strEstado = strEstado;
    }

	public String toString() {
        return ReflectionToStringBuilder.toString(this, ToStringStyle.SHORT_PREFIX_STYLE);
    }

	@Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "int_parte_id", columnDefinition = "INT")
    private Integer intParteId;

	public Integer getIntParteId() {
        return this.intParteId;
    }

	public void setIntParteId(Integer id) {
        this.intParteId = id;
    }

	@PersistenceContext
    transient EntityManager entityManager;

	public static final EntityManager entityManager() {
        EntityManager em = new Parte().entityManager;
        if (em == null) throw new IllegalStateException("Entity manager has not been injected (is the Spring Aspects JAR configured as an AJC/AJDT aspects library?)");
        return em;
    }

	public static long countPartes() {
        return entityManager().createQuery("SELECT COUNT(o) FROM Parte o", Long.class).getSingleResult();
    }

	public static List<Parte> findAllPartes() {
        return entityManager().createQuery("SELECT o FROM Parte o", Parte.class).getResultList();
    }

	public static Parte findParte(Integer intParteId) {
        if (intParteId == null) return null;
        return entityManager().find(Parte.class, intParteId);
    }

	public static List<Parte> findParteEntries(int firstResult, int maxResults) {
        return entityManager().createQuery("SELECT o FROM Parte o", Parte.class).setFirstResult(firstResult).setMaxResults(maxResults).getResultList();
    }

	@Transactional
    public void persist() {
        if (this.entityManager == null) this.entityManager = entityManager();
        this.entityManager.persist(this);
    }

	@Transactional
    public void remove() {
        if (this.entityManager == null) this.entityManager = entityManager();
        if (this.entityManager.contains(this)) {
            this.entityManager.remove(this);
        } else {
            Parte attached = Parte.findParte(this.intParteId);
            this.entityManager.remove(attached);
        }
    }

	@Transactional
    public void flush() {
        if (this.entityManager == null) this.entityManager = entityManager();
        this.entityManager.flush();
    }

	@Transactional
    public void clear() {
        if (this.entityManager == null) this.entityManager = entityManager();
        this.entityManager.clear();
    }

	@Transactional
    public Parte merge() {
        if (this.entityManager == null) this.entityManager = entityManager();
        Parte merged = this.entityManager.merge(this);
        this.entityManager.flush();
        return merged;
    }
}
